﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Rodunu.Movies.Data;
using Rodunu.Movies.Interfaces.User;

namespace Rodunu.Movies.Logic.User
{
    public class FriendsLogic : Repository<Contracts.Friends>, IFriends
    {
        #region IFriends Members

        public IList<Rodunu.Movies.Contracts.Friends> GetFriendsForUser(long userId)
        {
            ISessionFactory sessionFactory = DBHelper.BuildConfiguration().Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();

            System.Collections.Generic.IList<Contracts.Friends> friends = session.CreateCriteria(typeof(Contracts.Friends))
                .Add(Expression.Eq("UserInfo.Id", userId))
                .Add(Expression.Eq("Block", false))
                .Add(Expression.Eq("Pending", false))
                .List<Contracts.Friends>();

            //session.Close();


            return friends;
        }

        public IList<Contracts.Friends> GetPendingFreindsRequestsForUser(long userId)
        {
            ISessionFactory sessionFactory = DBHelper.BuildConfiguration().Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();

            System.Collections.Generic.IList<Contracts.Friends> friends = session.CreateCriteria(typeof(Contracts.Friends))
                .Add(Expression.Eq("Friend.Id", userId))
                .Add(Expression.Eq("Pending", true))
                .List<Contracts.Friends>();

            return friends;
        }

        public void SetFriend(long friendID, bool block)
        {
            ISession session = DBHelper.GetCurrentSession();
            IQuery query = session.GetNamedQuery("SetFriend");
            query.SetInt64("FriendId", friendID);
            query.SetBoolean("Block", block);
            query.UniqueResult();
        }

        #endregion
    }
}
